/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PremiumCustomerPanel.java
 *
 * Created on 7-Apr-2010, 10:26:54 AM
 */

package ticketmaster.presentation.customer;

import ticketmaster.util.*;
import java.sql.Timestamp;
import java.util.*;
import ticketmaster.accounting.TaxManager;
import ticketmaster.businesslogic.OrderController;
import ticketmaster.businessobject.LoginUser;
import ticketmaster.businessobject.PurchaseType;
import ticketmaster.businessobject.User;
import ticketmaster.common.OrderType;
import ticketmaster.common.PurchaseTypes;
import ticketmaster.dao.*;
import ticketmaster.dataobject.*;
import ticketmaster.presentation.customer.purchase.PaymentReviewView;

/**
 *
 * @author Xiaolong Xue
 */
public class PremiumCustomerPanel extends CustomerBasePanel
{

    /** Creates new form PremiumCustomerPanel */
    public PremiumCustomerPanel() 
    {
        super();

        User user = LoginUser.getUser();
        int userID = user.getUserID();
        PremiumCustomer premiumCustomer = new PremiumCustomer();
        premiumCustomer.setCustomerID(userID);
        DAOFactory.getInstance().getPremiumCustomerDAO().findPremiumCustomer(premiumCustomer);

        PremiumCustomerReward pcr = new PremiumCustomerReward();
        pcr.setCustomerID(premiumCustomer.getPremiumCustomerID());
        Collection<PremiumCustomerReward> pcrDO = DAOFactory.getInstance().getPremiumCustomerRewardDAO().getPremiumCustomerRewardsDO(pcr);
        Collection<Reward> rewards = new ArrayList<Reward>();
        for(PremiumCustomerReward preCustRwd : pcrDO)
        {
            Reward reward = new Reward();
            reward.setRewardID(preCustRwd.getRewardID());
            DAOFactory.getInstance().getRewardDAO().findReward(reward);
            rewards.add(reward);
        }
        
        initComponents();
        tblGiftcard = new GiftcardTable(rewards);
        add(tblGiftcard, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 380, -1, -1));

        getPremiumCustomerData();
    }

    private void getPremiumCustomerData()
    {
        User user = LoginUser.getUser();
        int userID = user.getUserID();
        PremiumCustomer premiumCustomer = new PremiumCustomer();
        premiumCustomer.setCustomerID(userID);
        DAOFactory.getInstance().getPremiumCustomerDAO().findPremiumCustomer(premiumCustomer);

        Timestamp expireDate = premiumCustomer.getExpireDate();
        Timestamp registrationDate = premiumCustomer.getRegistrationDate();
        int points = premiumCustomer.getPoints();
        txtTotalPoints.setText(Integer.toString(points));
        String strCycle = StringHelper.getDate(registrationDate.toString()).substring(5);
        txtBillingCycle.setText("Ends on " + strCycle + " of each year");

//      int curYear = Integer.parseInt(DateTimeHelper.getCurrentDateTime().toString().substring(0, 4));
        String expire = expireDate.toString().substring(0,10);
//        String due;
//        if(curYear == regYear)
//            due = Integer.toString(curYear + 1) + "-" + strCycle;
//        else
//            due = Integer.toString(curYear) + "-" + strCycle;
        txtPaymentDue.setText(expire);

        Bankcard bankcard = new Bankcard();
        bankcard.setCustomerID(userID);
        DAOFactory.getInstance().getBankcardDAO().findBankcard(bankcard);
        txtBankCardBalance.setText(Double.toString(bankcard.getBalance()));

        txtTotalDue.setText("$5.00");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        lblTotalDue = new javax.swing.JLabel();
        txtTotalDue = new javax.swing.JTextField();
        lblBillingCycle = new javax.swing.JLabel();
        lblPaymentDue = new javax.swing.JLabel();
        txtPaymentDue = new javax.swing.JTextField();
        btnPayNow = new javax.swing.JButton();
        txtBillingCycle = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        txtBankCardBalance = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        txtTotalPoints = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnRedeem = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ticketmaster.TicketMasterApp.class).getContext().getResourceMap(PremiumCustomerPanel.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel2.setBackground(resourceMap.getColor("jPanel2.background")); // NOI18N
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel2.border.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPanel2.border.border.titleFont")), "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPanel2.border.titleFont"))); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblTotalDue.setFont(resourceMap.getFont("lblTotalDue.font")); // NOI18N
        lblTotalDue.setText(resourceMap.getString("lblTotalDue.text")); // NOI18N
        lblTotalDue.setName("lblTotalDue"); // NOI18N
        jPanel2.add(lblTotalDue, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, -1, -1));

        txtTotalDue.setBackground(resourceMap.getColor("txtTotalDue.background")); // NOI18N
        txtTotalDue.setEditable(false);
        txtTotalDue.setFont(resourceMap.getFont("txtTotalDue.font")); // NOI18N
        txtTotalDue.setText(resourceMap.getString("txtTotalDue.text")); // NOI18N
        txtTotalDue.setName("txtTotalDue"); // NOI18N
        jPanel2.add(txtTotalDue, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 40, 73, -1));

        lblBillingCycle.setFont(resourceMap.getFont("lblTotalDue.font")); // NOI18N
        lblBillingCycle.setText(resourceMap.getString("lblBillingCycle.text")); // NOI18N
        lblBillingCycle.setName("lblBillingCycle"); // NOI18N
        jPanel2.add(lblBillingCycle, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 80, -1, -1));

        lblPaymentDue.setFont(resourceMap.getFont("lblTotalDue.font")); // NOI18N
        lblPaymentDue.setText(resourceMap.getString("lblPaymentDue.text")); // NOI18N
        lblPaymentDue.setName("lblPaymentDue"); // NOI18N
        jPanel2.add(lblPaymentDue, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, 161, 21));

        txtPaymentDue.setBackground(resourceMap.getColor("txtTotalDue.background")); // NOI18N
        txtPaymentDue.setEditable(false);
        txtPaymentDue.setFont(resourceMap.getFont("txtTotalDue.font")); // NOI18N
        txtPaymentDue.setText(resourceMap.getString("txtPaymentDue.text")); // NOI18N
        txtPaymentDue.setName("txtPaymentDue"); // NOI18N
        jPanel2.add(txtPaymentDue, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 120, 250, -1));

        btnPayNow.setFont(resourceMap.getFont("btnPayNow.font")); // NOI18N
        btnPayNow.setText(resourceMap.getString("btnPayNow.text")); // NOI18N
        btnPayNow.setName("btnPayNow"); // NOI18N
        btnPayNow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPayNowActionPerformed(evt);
            }
        });
        jPanel2.add(btnPayNow, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 80, 140, 70));

        txtBillingCycle.setBackground(resourceMap.getColor("txtTotalDue.background")); // NOI18N
        txtBillingCycle.setEditable(false);
        txtBillingCycle.setFont(resourceMap.getFont("txtTotalDue.font")); // NOI18N
        txtBillingCycle.setText(resourceMap.getString("txtBillingCycle.text")); // NOI18N
        txtBillingCycle.setName("txtBillingCycle"); // NOI18N
        jPanel2.add(txtBillingCycle, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 80, 250, -1));

        add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, 750, 160));

        jPanel3.setBackground(resourceMap.getColor("jPanel3.background")); // NOI18N
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel3.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPanel3.border.titleFont"), resourceMap.getColor("jPanel3.border.titleColor"))); // NOI18N
        jPanel3.setName("jPanel3"); // NOI18N
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel6.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N
        jPanel3.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, -1, 30));

        txtBankCardBalance.setBackground(resourceMap.getColor("txtBankCardBalance.background")); // NOI18N
        txtBankCardBalance.setEditable(false);
        txtBankCardBalance.setFont(resourceMap.getFont("txtBankCardBalance.font")); // NOI18N
        txtBankCardBalance.setText(resourceMap.getString("txtBankCardBalance.text")); // NOI18N
        txtBankCardBalance.setName("txtBankCardBalance"); // NOI18N
        jPanel3.add(txtBankCardBalance, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 50, 150, -1));

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        jPanel3.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 50, -1, 30));

        add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 180, 750, 90));

        jPanel4.setBackground(resourceMap.getColor("jPanel4.background")); // NOI18N
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel4.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPanel4.border.titleFont"))); // NOI18N
        jPanel4.setName("jPanel4"); // NOI18N
        jPanel4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel4.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N
        jPanel4.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, -1, 40));

        txtTotalPoints.setBackground(resourceMap.getColor("txtBankCardBalance.background")); // NOI18N
        txtTotalPoints.setEditable(false);
        txtTotalPoints.setFont(resourceMap.getFont("txtTotalPoints.font")); // NOI18N
        txtTotalPoints.setText(resourceMap.getString("txtTotalPoints.text")); // NOI18N
        txtTotalPoints.setName("txtTotalPoints"); // NOI18N
        jPanel4.add(txtTotalPoints, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 30, 120, 40));

        jLabel1.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel4.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 30, 50, 40));

        btnRedeem.setFont(resourceMap.getFont("btnRedeem.font")); // NOI18N
        btnRedeem.setText(resourceMap.getString("btnRedeem.text")); // NOI18N
        btnRedeem.setName("btnRedeem"); // NOI18N
        btnRedeem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRedeemActionPerformed(evt);
            }
        });
        jPanel4.add(btnRedeem, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 30, 140, 40));

        add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 280, 750, 90));
    }// </editor-fold>//GEN-END:initComponents

    private void btnRedeemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRedeemActionPerformed
        // TODO add your handling code here:
        RedeemPoint redeemPanel = new RedeemPoint();
        getPnlParent().removeAll();
        getPnlParent().add(redeemPanel);
        getPnlParent().validate();
        getPnlParent().repaint();
        redeemPanel.setPnlParent(getPnlParent());
    }//GEN-LAST:event_btnRedeemActionPerformed

    private void btnPayNowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPayNowActionPerformed
        // TODO add your handling code here:
        payMemberFee();
//      int expireYear =
    }//GEN-LAST:event_btnPayNowActionPerformed

    private void payMemberFee()
    {
        Order order = new Order();
        order.setCustomerID(LoginUser.getUser().getUserID());
        order.setOrderDate(DateTimeHelper.getCurrentDateTime());
        order.setOrderType(OrderType.YearlyFee.ordinal());
        order.setCost(YEARLYFEE + TaxManager.calculateTax(YEARLYFEE));
        order.setShippingMethodID(2);
        Event yearlyFeeEvent = new Event();
        yearlyFeeEvent.setName("Yearly Fee");
        yearlyFeeEvent.setCancelled(true);

        DAOFactory.getInstance().getEventDAO().getEventsDO(yearlyFeeEvent);
        order.setEventID(yearlyFeeEvent.getEventID());

        OrderController controller = new OrderController();
        order.setOrderID(controller.createOrder(order));
        if (order.getOrderID() > 0)
        {
            PaymentReviewView prv = new PaymentReviewView(YEARLYFEE, TaxManager.calculateTax(YEARLYFEE));
            prv.setOrder(order);
            prv.setBookedTickets(this.getBookedTickets());
            prv.setPnlParent(this.getPnlParent());
            PurchaseType.setPurchaseType(PurchaseTypes.Continue);
            this.getPnlParent().removeAll();
            this.getPnlParent().add(prv);
            this.getPnlParent().validate();
            this.getPnlParent().repaint();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPayNow;
    private javax.swing.JButton btnRedeem;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JLabel lblBillingCycle;
    private javax.swing.JLabel lblPaymentDue;
    private javax.swing.JLabel lblTotalDue;
    private javax.swing.JTextField txtBankCardBalance;
    private javax.swing.JTextField txtBillingCycle;
    private javax.swing.JTextField txtPaymentDue;
    private javax.swing.JTextField txtTotalDue;
    private javax.swing.JTextField txtTotalPoints;
    // End of variables declaration//GEN-END:variables
    private GiftcardTable tblGiftcard;
    static final double YEARLYFEE = 5.0;
}
